package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

//这里封装一个类,为数据库中博客表的增删查改,提供api
public class BlogDao {

//    1.新增一条博客(往数据库中新增一条数据)
    public void add(Blog blog) {
//        1.与数据库取得连接
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DBUtil.getConnection();
//            2.构造sql语句
//            因为blogId是自增的主键,所以不需要自己再设置
            String sql ="insert into blog values(null,?,?,?,?)";
            statement = connection.prepareStatement(sql);
//            3.设置sql语句中变量的值
            statement.setString(1,blog.getTitle());
            statement.setString(2,blog.getContent());
            statement.setTimestamp(3,blog.getPostTimestamp());
            statement.setInt(4,blog.getUserId());
//            4.执行sql语句
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
//            5.释放必要的资源
            DBUtil.close(connection,statement,null);
        }
    }

//    2.根据博客Id查询指定的博客(博客详情页会用到)
    public Blog selectById(int blogId) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
//            1.建立连接
            connection = DBUtil.getConnection();
//            2.构建sql语句
            String sql = "select * from blog where blogId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,blogId);
//            3.执行sql语句,并获取到结果集
            resultSet =statement.executeQuery();
//            4.遍历获取到的结果集
//            这里不用while的原因是查到的结果不是一条数据就是没有数据
            if(resultSet.next()) {
//                将结果集的值设置到blog中,并且返回
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blog.setUserId(resultSet.getInt("userId"));
                return blog;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
//            5.关闭资源
            DBUtil.close(connection,statement,resultSet);
        }
        return null;
    }

//    3.查询数据库中所有的博客列表(博客列表会用到)
    public List<Blog> selectAll() {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Blog> blogs = new ArrayList<>();
        try {
//            1.建立连接
            connection = DBUtil.getConnection();
//            2.构建sql语句
            String sql = "select * from blog order by postTime desc";
            statement = connection.prepareStatement(sql);
//            3.执行sql语句
            resultSet = statement.executeQuery();
//            4.遍历结果集
            while (resultSet.next()) {
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                String content = resultSet.getString("content");
//                这里博客列表页查询到的结果不需要全文显示,所以这里将获取到的文章内容进行截取
                if (content.length() >= 100) {
                    content = content.substring(0,100)+"...";
                }
                blog.setContent(content);
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blog.setUserId(resultSet.getInt("userId"));
                blogs.add(blog);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return blogs;
    }

//    4.删除指定的博客
    public void  delete(int blogId) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "delete from blog where blogId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(connection,statement,null);
        }
    }

}

